<template>
  <div class="category-list-box">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/',name:'Index'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row style="margin-top: 50px">
      <el-col :span="20" :offset="2">
        <el-button type="success" size="small" icon="el-icon-circle-plus-outline"
                   @click="goto('/admin/category/save','CategorySave')">添加
        </el-button>
      </el-col>
      <el-col :span="20" :offset="2">
        <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            label="序号"
            type="index"
            width="100">
          </el-table-column>
          <el-table-column
            prop="categoryName"
            label="分类名称"
            width="180">
          </el-table-column>
          <el-table-column
            label="创建时间"
            width="200">
            <template slot-scope="scope">
              <i class="el-icon-timer"></i>
              <span>{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="更新时间"
            width="200">
            <template slot-scope="scope">
              <i class="el-icon-time"></i>
              <span>{{ scope.row.updateTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="success"
                size="mini"
                icon="el-icon-edit"
                @click="edit(scope.row)">编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="delCategory(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="margin-top: 20px;"
          background
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="page.current"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog title="编辑分类" :visible.sync="dialogFormVisible">
      <el-form ref="categoryForm" :model="categoryForm" label-position="left" :rules="rules">
        <el-form-item label="分类名称" label-width="120" prop="categoryName">
          <el-input v-model.trim="categoryForm.categoryName" placeholder="请输入分类名称..."></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save('categoryForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "CategoryList",
  data() {
    return {
      dialogFormVisible: false,
      tableData: [],
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      categoryForm: {
        id: "",
        categoryName: "",
      },
      rules: {
        categoryName: [
          {required: true, message: "请输入分类名称", trigger: "blur"},
          {max: 50, message: "最多输入50个字符", trigger: "blur"},
        ]
      }
    }
  },
  created() {
    this.getTableData();
  },
  methods: {
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$post(this.$api.CATEGORY_UPDATE_URL, this.categoryForm).then(res => {
            if (res.data.code == 200) {
              this.$notify({
                title: "成功",
                message: res.data.message,
                type: "success"
              });
              this.dialogFormVisible = false;
              this.getTableData();
            }
          })
        }
      });
    },
    goto(path, name) {
      this.$router.push({
        path, name
      })
    },
    sizeChange(size) {
      this.page.size = size;
      this.getTableData();
    },
    currentChange(current) {
      this.page.current = current;
      this.getTableData();
    },
    getTableData() {
      this.$get(this.$api.CATEGORY_LIST_URL + `/${this.$store.getters.getUserInfo.id}/${this.page.current}/${this.page.size}`, {}).then(res => {
        if (res.data.code == 200) {
          this.tableData = res.data.data.records;
          this.page.total = res.data.data.total;
        }
      })
    },
    edit(data) {
      this.$nextTick(() => {
        this.$refs['categoryForm'].resetFields();
      })
      this.dialogFormVisible = true;
      let obj = JSON.parse(JSON.stringify(data));
      this.categoryForm.id = obj.id;
      this.categoryForm.categoryName = obj.categoryName;
    },
    delCategory(id) {
      this.$confirm("此操作将永久删除该分类，确定？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$del(this.$api.DELETE_CATEGORY_URL + `/${id}/${this.$store.getters.getUserInfo.id}`, {}).then(res => {
          if (res.data.code == 200) {
            this.$notify.success(res.data.message);
            this.getTableData();
          }
        })
      }).catch(() => {
        this.$notify.info("取消删除");
      })
    }
  }
}
</script>

<style scoped>

</style>
